﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ZhongKeSite.Common;
using ZhongKeSite.Entity;
using ZhongKeSite.Entity.Models;
using ZhongKeSite.Repository;

namespace ZhongKeSite.Service
{
    public class ExpertService : IExpertService
    {
        IExpertRepository expertRepository;
        public ExpertService(IExpertRepository _expertRepository)
        {
            expertRepository = _expertRepository;
        }

        #region Expert
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LayPager<Expert> GetExpertPagedList(int pageIndex, int pageSize, Expression<Func<Expert, bool>> predicate)
        {
            var list = expertRepository.GetPagedList(pageIndex, pageSize, predicate);
            LayPager<Expert> pmodel = new LayPager<Expert>();
            if (list != null)
            {
                pmodel.count = list.TotalItemCount;
                pmodel.msg = CommonConst.Success;
                pmodel.data = list.ToList();
            }
            return pmodel;
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultResponse<Expert> GetExpert(string id)
        {
            ResultResponse<Expert> resultResponse = new ResultResponse<Expert>() { success = false, msg = CommonConst.Fail };
            resultResponse.data = expertRepository.GetById(id);
            if (resultResponse.data != null)
            {
                resultResponse.success = true;
                resultResponse.msg = CommonConst.Success;
            }
            return resultResponse;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        public ResultResponse AddExpert(Expert model)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            if (string.IsNullOrWhiteSpace(model.ExpertName))
            {
                response.msg = "名字不能为空!";
                return response;
            }
            model.ID = IdentityCreator.NextIdentity;
            model.AddDate = DateTime.Now;
            response.success = expertRepository.Add(model);
            response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            return response;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity"></param>
        public ResultResponse UpdateExpert(Expert model)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            if (string.IsNullOrWhiteSpace(model.ExpertName))
            {
                response.msg = "名字不能为空!";
                return response;
            }
            var o = expertRepository.GetById(model.ID);
            o.ExpertName = model.ExpertName;
            o.Education = model.Education;
            o.Description = model.Description;
            o.AvatarImage = model.AvatarImage;
            o.Position = model.Position;
            o.SortNo = model.SortNo;
            o.UpdateBy = model.UpdateBy;
            o.UpdateDate = DateTime.Now;

            response.success = expertRepository.Update(o);
            response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            return response;
        }

        /// <summary>
        /// 推荐，取消推荐 操作
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="dataStatus"></param>
        /// <returns></returns>
        public ResultResponse UpdateExpertState(List<string> ids, string userName, DataStatus dataStatus)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            var products = expertRepository.GetList(s => ids.Contains(s.ID)).ToList();
            if (products != null && products.Count > 0)
            {
                switch (dataStatus)
                {
                    case DataStatus.Deleted:
                        products.ForEach(k =>
                        {
                            k.IsDel = 1;
                            k.UpdateBy = userName;
                            k.UpdateDate = DateTime.Now;
                        });
                        response.success = expertRepository.Update(products);
                        break;
                }
                response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            }
            return response;
        }
        #endregion
    }
}
